#include "DebugWidget.h"
#include <QPainter>
#include <QDebug>

DebugWidget::DebugWidget(QWidget *parent)
    : CommonContainer("调试区", false, 2, parent)
    , m_backBlowPressed(false)
    , m_bGunPressed(false)
    , m_returnDrySlightPressed(false)
    , m_returnDrySeverePressed(false)
    , m_topSprayPressed(false)
    , m_doubleSlagPressed(false)
{
    setupUI();
    setupConnections();
}

DebugWidget::~DebugWidget()
{
}

void DebugWidget::setupUI()
{
    // 创建按钮布局
    m_buttonLayout = new QVBoxLayout();
    m_buttonLayout->setContentsMargins(0, 0, 0, 0);
    m_buttonLayout->setSpacing(8);
    
    // 创建按钮
    QHBoxLayout *layout1 = new QHBoxLayout();
    layout1->setContentsMargins(0, 0, 0, 0);
    layout1->setSpacing(6);
    m_backBlowBtn = new QPushButton("后吹");
    m_backBlowBtn->setObjectName("debugButton");
    m_bGunBtn = new QPushButton("B枪");
    m_bGunBtn->setObjectName("debugButton");
    layout1->addWidget(m_backBlowBtn);
    layout1->addWidget(m_bGunBtn);
    
    m_returnDrySlightBtn = new QPushButton("返干—轻微");
    m_returnDrySlightBtn->setObjectName("debugButton");
    
    m_returnDrySevereBtn = new QPushButton("返干—重度");
    m_returnDrySevereBtn->setObjectName("debugButton");
    
    QHBoxLayout *layout2 = new QHBoxLayout();
    layout2->setContentsMargins(0, 0, 0, 0);
    layout2->setSpacing(6);
    m_topSprayBtn = new QPushButton("顶喷");
    m_topSprayBtn->setObjectName("debugButton");
    m_doubleSlagBtn = new QPushButton("双渣");
    m_doubleSlagBtn->setObjectName("debugButton");
    layout2->addWidget(m_topSprayBtn);
    layout2->addWidget(m_doubleSlagBtn);

    m_buttonLayout->addLayout(layout1);
    m_buttonLayout->addWidget(m_returnDrySlightBtn);
    m_buttonLayout->addWidget(m_returnDrySevereBtn);
    m_buttonLayout->addLayout(layout2);
    m_buttonLayout->addStretch();
    
    QWidget *buttonWidget = new QWidget();
    buttonWidget->setLayout(m_buttonLayout);
    
    setContentWidget(buttonWidget);
}

void DebugWidget::setupConnections()
{
    connect(m_backBlowBtn, &QPushButton::clicked, this, &DebugWidget::onBackBlowClicked);
    connect(m_bGunBtn, &QPushButton::clicked, this, &DebugWidget::onBGunClicked);
    connect(m_returnDrySlightBtn, &QPushButton::clicked, this, &DebugWidget::onReturnDrySlightClicked);
    connect(m_returnDrySevereBtn, &QPushButton::clicked, this, &DebugWidget::onReturnDrySevereClicked);
    connect(m_topSprayBtn, &QPushButton::clicked, this, &DebugWidget::onTopSprayClicked);
    connect(m_doubleSlagBtn, &QPushButton::clicked, this, &DebugWidget::onDoubleSlagClicked);
}

void DebugWidget::setButtonPressed(QPushButton* button, bool pressed)
{
    if (!button) return;
    
    if (pressed) {
        // 按下状态：使用更亮的颜色
        button->setStyleSheet(R"(
            QPushButton {
                background-color: #2B80FF;
                border: 1px solid #45BFFF;
                border-radius: 4px;
                color: #FFFFFF;
                font-size: 14px;
                font-weight: medium;
                padding: 8px 12px;
            }
            QPushButton:hover {
                background-color: #1A6BFF;
            }
        )");
    } else {
        // 未按下状态：恢复默认样式
        button->setStyleSheet(R"(
            QPushButton {
                background-color: #314C7A;
                border: 1px solid #93D2FF;
                border-radius: 4px;
                color: #FFFFFF;
                font-size: 14px;
                font-weight: medium;
                padding: 8px 12px;
            }
            QPushButton:hover {
                background-color: #4A6B9A;
            }
            QPushButton:pressed {
                background-color: #2A3C5A;
            }
        )");
    }
}

void DebugWidget::onBackBlowClicked()
{
    m_backBlowPressed = !m_backBlowPressed;
    setButtonPressed(m_backBlowBtn, m_backBlowPressed);
    emit buttonClicked(BackBlow, m_backBlowPressed);
}

void DebugWidget::onBGunClicked()
{
    m_bGunPressed = !m_bGunPressed;
    setButtonPressed(m_bGunBtn, m_bGunPressed);
    emit buttonClicked(BGun, m_bGunPressed);
}

void DebugWidget::onReturnDrySlightClicked()
{
    m_returnDrySlightPressed = !m_returnDrySlightPressed;
    setButtonPressed(m_returnDrySlightBtn, m_returnDrySlightPressed);
    emit buttonClicked(ReturnDrySlight, m_returnDrySlightPressed);
}

void DebugWidget::onReturnDrySevereClicked()
{
    m_returnDrySeverePressed = !m_returnDrySeverePressed;
    setButtonPressed(m_returnDrySevereBtn, m_returnDrySeverePressed);
    emit buttonClicked(ReturnDrySevere, m_returnDrySeverePressed);
}

void DebugWidget::onTopSprayClicked()
{
    m_topSprayPressed = !m_topSprayPressed;
    setButtonPressed(m_topSprayBtn, m_topSprayPressed);
    emit buttonClicked(TopSpray, m_topSprayPressed);
}

void DebugWidget::onDoubleSlagClicked()
{
    m_doubleSlagPressed = !m_doubleSlagPressed;
    setButtonPressed(m_doubleSlagBtn, m_doubleSlagPressed);
    emit buttonClicked(DoubleSlag, m_doubleSlagPressed);
}
